package com.ycode.bang.anbang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.utils.SlideSwitch;
import com.ycode.bang.anbang.utils.StringUtils;
import com.ycode.bang.anbang.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 编辑地址/公共报修部分
 */
public class EditAddressActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.lin_infor)
    LinearLayout lin_Infor;//编辑的地址
    @InjectView(R.id.Sw_switch)
    Switch mSwitch;//代申报切换按钮
    @InjectView(R.id.slideSW)
    SlideSwitch mSw;//代申报切换按钮
    @InjectView(R.id.lin_Selectxiaoqu)
    LinearLayout lin_Select;//选择小区
    @InjectView(R.id.et_name)
    EditText mEtName;//姓名
    @InjectView(R.id.xiaoqu)
    TextView xiaoqu;//小区
    @InjectView(R.id.et_number)
    EditText mEtNumber;//电话
    @InjectView(R.id.btn_ok)
    Button mBt_Ok;//

    @InjectView(R.id.lin)
    LinearLayout lin;//代申报的那个

    private int tag = 1;
    private int sw = 1;//标记代申报切换按钮是否打开，1是关闭，0是打开
    private String helpApply = "false";//是否是代业主报修
    private String plotid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_address);
        ButterKnife.inject(this);
        Bundle bundle = this.getIntent().getExtras();
        tag = bundle.getInt("tag");
        initView();
    }

    public void initView() {
        if (tag == 4) {
            lin.setVisibility(View.GONE);
            lin_Infor.setVisibility(View.VISIBLE);
        }
        title.setText("编辑地址");
        mSwitch.setOnClickListener(this);
        mSw.setOnClickListener(this);
        lin_Select.setOnClickListener(this);
        mBt_Ok.setOnClickListener(this);

        mSw.setOnSwitchChangedListener(new SlideSwitch.OnSwitchChangedListener() {
            @Override
            public void onSwitchChanged(SlideSwitch obj, int status) {
                if (status == 1) {
                    lin_Infor.setVisibility(View.VISIBLE);
                    sw = 0;
                    helpApply = "true";
                } else {
                    lin_Infor.setVisibility(View.GONE);
                    sw = 1;
                    helpApply = "false";
                }
            }
        });

        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    lin_Infor.setVisibility(View.VISIBLE);
                } else {
                    lin_Infor.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_Selectxiaoqu:
                Intent intent = new Intent(this, SelectPlotActivity.class);
                bundle.putString("plotid", plotid);
                intent.putExtras(bundle);
                startActivityForResult(intent, 2016);
                break;
            case R.id.btn_ok:
                if (sw == 0 || tag == 4) {
                    Intent intent2 = new Intent();
                    intent2.setClass(this, CreatePublicActivity.class);
                    Bundle bundle2 = new Bundle();
                    if (TextUtils.isEmpty(xiaoqu.getText().toString().trim())) {
                        ToastUtil.show(this, "请选择小区");
                        return;
                    }
                    if (TextUtils.isEmpty(mEtName.getText().toString().trim())) {
                        ToastUtil.show(this, "请输入姓名");
                        return;
                    }
                    if (TextUtils.isEmpty(mEtNumber.getText().toString().trim())) {
                        ToastUtil.show(this, "请输入手机号码");
                        return;
                    }
                    if (!StringUtils.isPhone(mEtNumber.getText().toString().trim())) {
                        ToastUtil.show(this, "请输入正确的手机号码");
                        return;
                    }
                    bundle2.putString("plot", xiaoqu.getText().toString().trim());
                    bundle2.putString("number", mEtNumber.getText().toString().trim());
                    bundle2.putString("name", mEtName.getText().toString().trim());
                    bundle2.putString("helpApply", "false");
                    bundle2.putString("plotid", plotid);
                    intent2.putExtra("bundle2", bundle2);
                    setResult(Activity.RESULT_OK, intent2);
                    finish();
                } else {
                    Intent intent2 = new Intent();
                    intent2.setClass(this, CreatePublicActivity.class);
                    Bundle bundle2 = new Bundle();
                    bundle2.putString("plot", xiaoqu.getText().toString().trim());
                    bundle2.putString("number", mEtNumber.getText().toString().trim());
                    bundle2.putString("name", mEtName.getText().toString().trim());
                    bundle2.putString("helpApply", "true");
                    bundle2.putString("plotid", plotid);
                    intent2.putExtra("bundle2", bundle2);
                    setResult(Activity.RESULT_OK, intent2);
                    finish();
                }

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case 2016:
//                    xiaoqu.setText(data.getStringExtra("plot"));
                    Bundle bundle = data.getBundleExtra("bundle");
                    xiaoqu.setText(bundle.getString("plot"));
                    plotid = bundle.getString("plotid");
//                    plotid=bundle.getString("plotid");
                    break;
            }
        }
    }
}
